//
//  FamilyAddressAddCell.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/3/12.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

enum FamilyAddressAddCellType: Int {
    case name     = 0
    case phone
    case area
    case address
    case commonly
    case sure // 确定
    
    var placeholder: String {
        switch self {
        case .name: return "请输入姓名"
        case .phone: return "请输入手机号"
        case .area: return "请选择省市区"
        case .address: return "街道、小区/村、门牌号"
        default: return ""
        }
    }
}

protocol FamilyAddressAddCellDelegate: NSObjectProtocol {
    func addressAddCell(_ cell: FamilyAddressAddCell, didEndEditing vm: FamilyAddViewModel)
    func addressAddCell(_ cell: FamilyAddressAddCell, didClickedCommonly vm: FamilyAddViewModel)
    func addressAddCell(didClickedSure cell: FamilyAddressAddCell)
}

class FamilyAddressAddCell: UITableViewCell {
    
    weak var delegate: FamilyAddressAddCellDelegate?
    private var vm = FamilyAddViewModel(model: FamilyAddModel())
    private var cellType: FamilyAddressAddCellType = .name {
        didSet {
            initUI(with: cellType)
        }
    }
    
    @IBOutlet private weak var titleLabel: UILabel!
    @IBOutlet private weak var textField: UITextField! {
        didSet {
            textField.delegate = self
        }
    }
    @IBOutlet private weak var arrowView: UIImageView!
    @IBOutlet private weak var commonlyBtn: UIButton!
    @IBOutlet private weak var splitLine: UIView!
    @IBOutlet private weak var sureBtn: UIButton! {
        didSet {
            sureBtn.setBackgroundImage(UIImage(color: .mainColor), for: .normal)
            sureBtn.setBackgroundImage(UIImage(color: .mainLight), for: .highlighted)
            sureBtn.layer.cornerRadius = sureBtn.height * 0.5
            sureBtn.layer.masksToBounds = true
        }
    }
    
    @IBAction private func commonly(_ sender: UIButton) {
        textField.resignFirstResponder()
        sender.isSelected.toggle()
        vm.isDefault = sender.isSelected ? 1 : 0
        delegate?.addressAddCell(self, didClickedCommonly: vm)
    }
    
    @IBAction private func sure(_ sender: UIButton) {
        delegate?.addressAddCell(didClickedSure: self)
    }
    
    func configure(with vm: FamilyAddViewModel, cellType: FamilyAddressAddCellType) {
        self.vm = vm
        self.cellType = cellType
        
        switch cellType {
        case .name:
            textField.text = vm.receiveName
        case .phone:
            textField.text = vm.receivePhone
        case .area:
            textField.text = vm.area
        case .address:
            textField.text = vm.addr
        case .commonly:
            commonlyBtn.isSelected = vm.isCommonly
        default: break
        }
    }
}

extension FamilyAddressAddCell {
    private func initUI(with cellType: FamilyAddressAddCellType) {
        titleLabel.text = vm.addresses[cellType.rawValue]
        textField.placeholder = cellType.placeholder
        switch cellType {
        case .area:
            setupAreaCell()
        case .phone:
            textField.keyboardType = .phonePad
        case .commonly:
            setupCommonlyCell()
        case .sure:
            setupSureCell()
        default: break
        }
    }
    
    private func setupAreaCell() {
        arrowView.isHidden  = false
        textField.isEnabled = false
    }
    
    private func setupCommonlyCell() {
        textField.isHidden   = true
        commonlyBtn.isHidden = false
    }
    
    private func setupSureCell() {
        titleLabel.isHidden = true
        textField.isHidden  = true
        splitLine.isHidden  = true
        sureBtn.isHidden    = false
    }
}

extension FamilyAddressAddCell: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        switch cellType {
        case .name: vm.receiveName = textField.text!
        case .phone: vm.receivePhone = textField.text!
        case .address: vm.addr = textField.text!
        default: break
        }
        
        delegate?.addressAddCell(self, didEndEditing: vm)
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if cellType == .phone {
            let totalLength = (textField.text?.lengthOfBytes(using: .utf8))! - range.length + string.lengthOfBytes(using: .utf8)
            if totalLength > 11 { return false }
            let lengthOfStr = string.lengthOfBytes(using: .utf8)
            for index in 0..<lengthOfStr {
                let chara = (string as NSString).character(at: index)
                // 48-57;{0,9};65-90;{A..Z};97-122:{a..z}
                if chara < 48 || chara > 57 { return false }
            }
            return true
        }
        return true
    }
}
